﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using ClasesLibreria.dominio;

namespace ClasesLibreria.dao
{
    public static class DAOCargo
    {
        public static SqlDataReader queryCargos(string connectionString)
        {
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader dr;

            conn = new SqlConnection();
            conn.ConnectionString = connectionString;
            conn.Open();
            cmd = new SqlCommand("SELECT * FROM cargo ORDER BY nombre");
            cmd.Connection = conn;
            dr = cmd.ExecuteReader();

            return dr;
        }

        public static Cargo getById(int id, string connectionString)
        {
            Cargo cargo = new Cargo();
            SqlConnection conn;
            SqlCommand cmd;
            SqlDataReader dr;

            conn = new SqlConnection();
            conn.ConnectionString = connectionString;
            conn.Open();
            cmd = new SqlCommand("SELECT IdCargo, Nombre, Descripcion FROM cargo WHERE IdCargo = @IdCargo");
            cmd.Connection = conn;
            cmd.Parameters.Add(new SqlParameter("@IdCargo", id));
            dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                cargo.Id = int.Parse(dr["IdCargo"].ToString());
                cargo.Nombre = dr["Nombre"].ToString();
                cargo.Descripcion = dr["Descripcion"].ToString();
                break;
            }
            conn.Close();
            return cargo;
        }
    }
}
